let timer;
let timerDebounce;
let flag;
export const $tool = {
	//图片预览
	previewImage: (url) => {
		uni.previewImage({
			current: 0,
			urls: [url]
		})
	},
	//提示
	toast: (str) => {
		uni.showToast({
			title: str,
			icon: 'none'
		})
	},
	daysUntilDate: (m_d) => {
		let str = m_d.substring(5)
		// 获取当前日期
		const currentDate = new Date();

		// 分割输入的日期字符串
		const [month, day] = str.split('-').map(Number);

		// 获取当前年份
		const currentYear = currentDate.getFullYear();

		// 创建目标日期对象
		const targetDate = new Date(currentYear, month - 1, day);

		// 如果目标日期已经过去，计算下一个年份的日期
		if (targetDate < currentDate) {
			targetDate.setFullYear(currentYear + 1);
		}

		// 计算时间差并转换为天数
		const diffTime = targetDate - currentDate;
		const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

		return diffDays;
	},
	//复制
	copy: (str) => {
		uni.setClipboardData({
			data: str
		})
	},
	//节流函数 
	throttle: (func, wait = 500, immediate = true) => {
		if (immediate) {
			if (!flag) {
				flag = true
				typeof func === 'function' && func()
				timer = setTimeout(() => {
					flag = false
				}, wait)
			}
		} else if (!flag) {
			flag = true
			timer = setTimeout(() => {
				flag = false
				typeof func === 'function' && func()
			}, wait)
		}
	},
	debounce: (func, wait = 500, immediate = false) => {
		if (timerDebounce !== null) clearTimeout(timerDebounce)
		if (immediate) {
			const callNow = !timerDebounce
			timerDebounce = setTimeout(() => {
				timerDebounce = null
			}, wait)
			if (callNow) typeof func === 'function' && func()
		} else {
			timerDebounce = setTimeout(() => {
				typeof func === 'function' && func()
			}, wait)
		}
	}
}